def login():
    print('{0:*^50}'.format('三乐购物系统'))
    dict1 = {'root': 123456, 'chen': 12345678}
    count = 0
    while True:
        username = input('请输入用户名：')
        if username in dict1.keys():
            passwd = int(input('请输入密码：'))
            if passwd == dict1[username]:
                print("登录成功！")
                break
            else:
                count += 1
                print(f'密码错误,你还有{3-count}次机会输入')
        else:
            count += 1
            print(f'用户名错误,你还有{3-count}次机会输入')
        if count == 3:
            break


dict_goods ={'F001': {'name': '牛奶', 'price': 20}, 'F002': {'name': '鞋子', 'price': 100},
             'F003': {'name': '香水', 'price': 200}, 'F004': {'name': '帽子', 'price': 30},
             'F005': {'name': '香蕉', 'price': 2}, 'F006': {'name': '苹果', 'price': 10},
             'F007': {'name': '咖啡', 'price': 8},  'F008': {'name': '葡萄', 'price': 9} }
#查看商品信息
def goods():
    print('商品编号****商品名称****商品价格')
    for good in dict_goods:
        print(f'{good:<10}{dict_goods[good]["name"]:<12}{dict_goods[good]["price"]:<10}')


#查看购买清单
def inventory(shopping_list):
    print('查看购买清单')
    print('商品编号****商品名称****商品价格****数量')
    for item in shopping_list:
        print(f'{item:<10}{dict_goods[item]["name"]:<12}{dict_goods[item]["price"]:<12}'
        f'{shopping_list[item]}')



def shopping(balance,shopping_list):
    while True:
        number = input('请输入商品编号：')
        if number in dict_goods:
            print('开始购买商品')
            quantity = input('请输入商品数量：')
            if quantity.isdigit():
                quantity = int(quantity)
                if quantity * dict_goods[number]['price'] <= balance:
                    if number in shopping_list:
                        shopping_list[number] += quantity
                    else:
                        shopping_list[number] = quantity
                    balance -= quantity * dict_goods[number]['price']
                    print('购买成功，请继续！')
                    continue
                else:
                    print('当前余额不足！请充值')
                    money = input('请输入充值金额：')
                    if money.isdigit():
                        money = int(money)
                        balance += money
            else:
                print('输入有误！')
        else:
            print('商品编号错误')

        return balance


def menu():
    login()
    print('{0:*^20}'.format('欢迎登录三乐购物系统'))
    money = input('请充值：')
    if money.isdigit():
        money = float(money)
        balance = money
    else:
        print('充值失败！')
        balance = 0
    print(f'您的余额为：{balance}')
    shopping_list = {}
    while True:
        print("-"*20)
        print("1.{0:*>20}".format("商品详情"))
        print("2.{0:*>20}".format("购买商品"))
        print("3.{0:*>20}".format("购买清单"))
        print("4.{0:*>20}".format("查看余额"))
        print("5.{0:*>20}".format("退出系统"))
        print('-'*20)
        s = input('请输入您需要的选项：')
        if s == '1':
            print('查看商品详细信息')
            goods()
        elif s =='2':
            print('购买商品')
            balance = shopping(balance, shopping_list)
        elif s == '3':
            inventory(shopping_list)
        elif s == '4':
            print('查看商品余额')
            print(f'您目前的余额为：{float("%.2f" % balance)}')
        elif s == '5':
            break
        else:
            print('输入错误！请重新输入')

    print('您购买的商品为：')
    print('商品编号*****商品名称*****商品价格*****数量')
    for item in shopping_list:
        print(f'{item:<10}{dict_goods[item]["name"]:<12}{dict_goods[item]["price"]:<12}'
              f'{shopping_list[item]}')
    print(f'您的余额为：{balance}')
    print('欢迎下次光临三乐购物系统！！')


if __name__ == "__main__":
    menu()
